package imgshare.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import imgshare.common.api.CommonResult;
import imgshare.model.Category;
import imgshare.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    private static final Log log = LogFactory.get();

    @GetMapping
    public CommonResult<List<Category>> list() {

        List<Category> categoryList = categoryService.findAll();

        return CommonResult.success(categoryList);
    }

    @PostMapping("/create")
    public CommonResult<?> create(@RequestBody @Validated Category category) {

        categoryService.create(category);
        log.info("类别【{}】创建成功",category.getName());
        return CommonResult.success();
    }
}
